package com.yunyike.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunyike.constants.CartConstants;
import com.yunyike.constants.CommonConstant;
import com.yunyike.dto.SettlementDTO;
import com.yunyike.mapper.IdentityCardMapper;
import com.yunyike.model.IdentityCard;
import com.yunyike.model.Recepient;
import com.yunyike.redis.JedisManager;
import com.yunyike.service.IIdentityCardService;


@Service("identityCardServiceImpl")
public class IdentityCardServiceImpl implements IIdentityCardService {

	private static Logger log = LoggerFactory.getLogger(IdentityCardServiceImpl.class);
	
	@Autowired
	private IdentityCardMapper identityCardMapper;

	public int deleteByPrimaryKey(String identityCardId) {
		return identityCardMapper.deleteByPrimaryKey(identityCardId);
	}

	public int insert(IdentityCard record) {
		return identityCardMapper.insert(record);
	}

	public int insertSelective(IdentityCard record) {
		return identityCardMapper.insertSelective(record);
	}

	public IdentityCard selectByPrimaryKey(String identityCardId) {
		return identityCardMapper.selectByPrimaryKey(identityCardId);
	}

	public int updateByPrimaryKeySelective(IdentityCard record) {
		return identityCardMapper.updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(IdentityCard record) {
		return identityCardMapper.updateByPrimaryKey(record);
	}
	
	public List<IdentityCard> selectByIdentityCard(IdentityCard idcard){
		return identityCardMapper.selectByIdentityCard(idcard);
	}

	public IdentityCard getDefaultIdentityCard(String userId){
		IdentityCard idcard = this.selectDefaultIDCard(userId);
		if(idcard!=null)
			return idcard;
		else {
			IdentityCard param = new IdentityCard();
			param.setUserId(userId);
			param.setIsDefault(CommonConstant.NUMBER_1);
			List<IdentityCard> list = identityCardMapper.selectByIdentityCard(param);
			if(list!=null&&list.size()>0)return list.get(0);
			else return null;
		}
	}
	
	
	public int setDefaultIdentityCard(IdentityCard record) {
		IdentityCard param = new IdentityCard();
		param.setUserId(record.getUserId());
		param.setIdentityCardStatus(CommonConstant.NUMBER_1);
		List<IdentityCard> list = identityCardMapper.selectByIdentityCard(param);
		if (list != null && list.size() > 0) {
			for (IdentityCard card : list) {
				card.setIsDefault(CommonConstant.NUMBER_0);
				identityCardMapper.updateByPrimaryKeySelective(card);
			}
		}
		return identityCardMapper.updateByPrimaryKeySelective(record);
	}
	public  IdentityCard selectDefaultIDCard(String userId){
		return identityCardMapper.selectDefaultIDCard(userId);
	}
	
	/**
	 * 选择IDCARD
	 */
	public int chooseIDCard(IdentityCard idcard , String sessionId) {
		
		log.info("choose idcard...");
		
		// settlement redis key
		log.info("----->> generator settlement key...");
		String settlementKey = CartConstants.settlementKey(sessionId);
		log.info("settlement key:"+settlementKey);
		
		// 获取当前结算信息
		log.info("get settlement data from redis...");
		try {
			SettlementDTO settlementDTO = (SettlementDTO)JedisManager.getObject(settlementKey);
			settlementDTO.setIdCard(idcard);
			JedisManager.setObject(settlementKey, CartConstants.SETTLEMENT_OUT_TIME, settlementDTO);
		} catch (Exception e) {
			log.error("chooseIDCard has error,message:"+e.getMessage(),e);
			return CommonConstant.NUMBER_0;
		}
		
		log.info("choose idcard end.");
		return CommonConstant.NUMBER_1;
	}
	/**
	 * 判断当前idcard身份证信息是否是结算信息当前选中的idcard
	 */
	public boolean checkIDCard(IdentityCard idcard , String sessionId){

		log.info("checkIDCard ...");
		
		// settlement redis key
		log.info("----->> generator settlement key...");
		String settlementKey = CartConstants.settlementKey(sessionId);
		log.info("settlement key:"+settlementKey);
		
		// 获取当前结算信息
		log.info("get settlement data from redis...");
		try {
			SettlementDTO settlementDTO = (SettlementDTO)JedisManager.getObject(settlementKey);
			if(settlementDTO!=null){
				IdentityCard sessionIDCard = settlementDTO.getIdCard();
				if(sessionIDCard!=null&&
						idcard.getIdentityCardId().equals(sessionIDCard.getIdentityCardId())){
					log.info("checkIDCard  ... this idcard is the currently selected");
					return true;
				}
			}
		} catch (Exception e) {
			log.error("chooseIDCard has error,message:"+e.getMessage(),e);
			return false;
		}
		
		log.info("checkIDCard  end.");
		return false;
	}
	
	
	public IdentityCard getIdentityCardByName(String receivingName){
		IdentityCard param = new IdentityCard();
		param.setIdentityCardName(receivingName);
		param.setIdentityCardStatus(CommonConstant.NUMBER_1);
		List<IdentityCard> idcards = identityCardMapper.selectByIdentityCard(param);
		if(idcards!=null&&idcards.size()>0){
			return idcards.get(0);
		}
		return null;
	}
	
	
}
